﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Designit.Umb.DataTypes.Video
{
    public class DataEditor : Panel, umbraco.interfaces.IDataEditor
    {
        private readonly umbraco.interfaces.IData data;
        private TextBox embeddedVideoUrl;
        private Literal embeddedVideoPreview;
        private string divId;

        public DataEditor(umbraco.interfaces.IData Data)
        {
            data = Data;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            embeddedVideoUrl = new TextBox();
            embeddedVideoPreview = new Literal();
            divId = Guid.NewGuid().ToString();

            if (data != null && !string.IsNullOrEmpty(data.Value.ToString()))
            {
                embeddedVideoUrl.Text = data.Value.ToString();
                embeddedVideoPreview.Text =
                    string.Format("<div id=\"{0}\"></div>", divId);
            }
            else
            {
                embeddedVideoUrl.Text = string.Empty;
                embeddedVideoPreview.Text = string.Format("<div id=\"{0}\" style=\"display:none;\">&nbsp;</div>", divId);
            }

            embeddedVideoUrl.TextMode = TextBoxMode.SingleLine;
            embeddedVideoUrl.CssClass = "umbEditorTextField";
            embeddedVideoUrl.Attributes.Add("onchange", "previewVideo(this, '" + divId + "')");

            Controls.Add(embeddedVideoUrl);
            Controls.Add(new LiteralControl("<br /><br />"));
            Controls.Add(embeddedVideoPreview);

            Page.ClientScript.RegisterClientScriptBlock(GetType(), "EmbeddedVideo", EmbedVideo());
        }

        public System.Web.UI.Control Editor
        {
            get { return this; }
        }

        public void Save()
        {
            data.Value = embeddedVideoUrl.Text;
        }

        public bool ShowLabel
        {
            get { return true; }
        }

        public bool TreatAsRichTextEditor
        {
            get { return false; }
        }

        public string EmbedVideo()
        {
            var sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\">");

            sb.AppendLine("$(document).ready(function () {");
            sb.AppendLine("  previewVideo(document.getElementById(\"" + embeddedVideoUrl.ClientID + "\"), \"" + divId + "\");");
            sb.AppendLine("});");
            
            sb.AppendLine("function getVideoLink(v) {");
            sb.AppendLine("  // YouTube");
            sb.AppendLine("  if (v.match(/watch\\?v=(.+)(.*)/)) {");
            sb.AppendLine("  var yid = v.replace(/^[^v]+v.(.{11}).*/,'$1\');");
            sb.AppendLine("    return 'http://www.youtube.com/v/' + yid;");
            sb.AppendLine("  }");
            sb.AppendLine("  // Vimeo");
            sb.AppendLine(@"  if ( v.match(/^http:\/\/(?:www\.){0,1}vimeo\.com\/(\d+)$/) ) {");
            sb.AppendLine(@"    return 'http://vimeo.com/moogaloop.swf?clip_id=' + v.match(/^http:\/\/(?:www\.){0,1}vimeo\.com\/(\d+)$/)[1];");
            sb.AppendLine("  }");
            sb.AppendLine("  // Google video");
            sb.AppendLine("  if (v.indexOf('http://video.google.com/videoplay?docid=') == 0) {");
            sb.AppendLine("    return 'http://video.google.com/googleplayer.swf?docId=' + v.substring('http://video.google.com/videoplay?docid='.length) + '&hl=en';");
            sb.AppendLine("  }");
            sb.AppendLine("}");

            sb.AppendLine("function previewVideo(obj, divId){");
            sb.AppendLine("var objValue = obj.value;");
            sb.AppendLine("$('#' + divId).css('display', 'block');");
            sb.AppendLine("var url = getVideoLink(objValue);");
            sb.AppendLine("var embedCode = '<object width=\"320\" height=\"240\"><param name=\"movie\" value=\"' + url + '\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"' + url + '\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"320\" height=\"240\"></embed></object>';");
            sb.AppendLine("$('#' + divId).html(embedCode);");
            sb.AppendLine("}");

            sb.AppendLine("</script>");
            
            return sb.ToString();

        }
      
    }
}